package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityUserSecondLeftByHour;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-分时次留manager
 * 
 * @author xckuang
 * @date 2016-11-10
 */
@Service
public class UserSecondLeftByHourManager extends ManagerQueryCacheBase<EntityUserSecondLeftByHour> {
	private static final long serialVersionUID = 1L;
	//platformid@channelid@serverid_hour
	private static final String KeyPatten = "%s@%s@%s_%s";
    private static DecimalFormat PERCENT_DF = new DecimalFormat("0.00%");
	
	private String format(String val){
		return null != val ? val : "";
	}
	
	public String getKey(String plat,String chan,String serv, int hour){
		return String.format(KeyPatten, format(plat),format(chan),format(serv),hour);
	}
	
	// 得到符合平台、渠道、区服条件的查看日次留总和
	public Map<String,Object[]> getAllUserSecondLeftByHour(Date lookDate) {
		List<Object[]> list = findByQuery("select '0',hour,usercreatenum,user_second_left from EntityUserSecondLeftByHour where day=:dayStart and platformid='0' and channelid ='0' and serverid = '0' order by hour", "dayStart", lookDate);
		return getResult(list);
	}
	
	// 根据查询结果，拼装最终结果
	// curkey key hour usercreatenum user_second_left
	// key,Object[] = 24*2+1元素
	private Map<String, Object[]> getResult(List<Object[]> list) {
		Map<String, Object[]> result = new LinkedHashMap<>();
		if (list == null || list.isEmpty() || list.get(0)[0] == null) {
			return result;
		}
		for (Object[] o : list) {
			String key = o[0].toString();
			Object[] val = result.get(key);
			if (null == val) {
				val = new Object[49];
				for (int index = 0; index < val.length; ++index) {
					val[index] = (index % 2 == 0 ? "0.00%" : Integer.valueOf(0));
				}
				val[0] = key;
				result.put(key, val);
			}
			Integer hour = Integer.valueOf(o[1].toString());
			val[2 * hour + 1] = String2Integer(o[2].toString());
			val[2 * hour + 2] = double2Percent(Double.valueOf(o[3].toString()));
		}
		return result;
	}
	
	private static Integer String2Integer(String d) {
        return NumberUtils.isNumber(d.toString()) ? Integer.valueOf(d) : 0;
    }

    private static String double2Percent(Double d) {
        d = NumberUtils.isNumber(d.toString()) ? d : 0;
        return PERCENT_DF.format(d);
    }
	
	public Map<String,Object[]> getPaltform2LeftRealTimeDatas(Date lookDate) {
		List<Object[]> list =  findByQuery("select platformid,hour,usercreatenum,user_second_left from EntityUserSecondLeftByHour where day=:dayStart and platformid !='0' and channelid ='0' and serverid = '0' order by platformid,hour", "dayStart", lookDate);
		return getResult(list);
	}
	
	public Map<String,Object[]> getChannel2LeftRealTimeDatas(Date lookDate) {
		List<Object[]> list = findByQuery("select channelid,hour,usercreatenum,user_second_left from EntityUserSecondLeftByHour where day=:dayStart and platformid='0' and channelid !='0' and serverid = '0' order by channelid,hour", "dayStart", lookDate);
		return getResult(list);
	}
	
	public Map<String,Object[]> getServer2LeftRealTimeDatas(Date lookDate) {
		List<Object[]> list = findByQuery("select serverid,hour,usercreatenum,user_second_left from EntityUserSecondLeftByHour where day=:dayStart and platformid='0' and channelid ='0' and serverid != '0' order by serverid,hour", "dayStart", lookDate);
		return getResult(list);
	}
}
